<html>
<head>
	<script type="text/javascript">
		var aJaxURL			= "server-side/operations/describe.action.php";		//server side folder url
		var list_aJaxURL	= "server-side/operations/describe/list.action.php";	//server side folder url
		var seoyURL			= "server-side/seoy/seoy.action.php";					//server side folder url
		var tName			= "example";													//table name
		var fName			= "add-edit-form";												//form name

		$(document).ready(function () {
            ClearDB();

			LoadTable(tName);

			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "");

			SetEvents("add_button", "", "", tName, fName, aJaxURL);
		});

        //SeoYyy
		$(document.body).click(function (e) {
        	$("#trans_production").autocomplete("close");
        });

		function LoadTable(table){
			if(table == "prod_list"){
				var menuLength	= [[10], [10]];

				var local_id	= $("#describe_id").val();

				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable1("prod_list", list_aJaxURL, "get_list", 4, "local_id=" + local_id, 0, menuLength, 1, "asc");
			}else{
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
                GetDataTable(tName, aJaxURL, "get_list", 5, "", 0, "", 0, "desc");
			}
		}

		function LoadDialog(form){
			if(form == fName){
				ClearDB();

				var buttons = {
						"print_describe": {
				            text: "ბეჭდვა",
				            id: "print_describe",
				            click: function () {
				            }
				        },
				        "end-describe": {
				            text: "აღწერის დასრულება",
				            id: "end_describe",
				            click: function () {
				            }
				        },
				        "save": {
				            text: "შენახვა",
				            id: "save-dialog",
				            click: function () {
				            }
				        },
				        "cancel": {
				            text: "დახურვა",
				            id: "cancel-dialog",
				            click: function () {
					            $(this).dialog("close");
				            }
				        }
				 };

				/* Dialog Form Selector Name, Buttons Array */
				GetDialog(fName, 600, "auto", buttons);
				var id	= $("#describe_id").val();

				if(empty(id)){
					var local_id	= GetLocalID();
					$("#describe_id").val(local_id);
				}else{
					$("#object").attr("disabled", "disabled");
					$("#person").attr("disabled", "disabled");
					$("#save-dialog").button("disable");
				}

				if( $("#describe_status").val() == 1 ){
					$("#end_describe").button("disable");
				}

				LoadTable("prod_list");
				GetButtons("add_button1", "del_button1");
				SetEvent();

			}else{

				var id = $("#describe_id").val();
				var ob = $("#object").val();
				SeoY("production", seoyURL, "describe_production", "des="+ id + "&ob="+ ob, 0);
				var buttons = {
			        "save": {
			            text: "დამატება",
			            id: "add-production",
			            click: function () {
			            }
			        }
			    };
				GetDialog("add-edit-prod-form", 500, "auto", buttons);
				$("#quantity").attr("disabled", "disabled");

			}
		}

		function CloseDialog(form){
			$("#" + form).dialog("close");
		}

		function SetEvent(){
		    $("#del_button1").on("click", function () {
		        var data = $(".check:checked").map(function () { //Get Checked checkbox array
		            return this.value;
		        }).get();

		        for (var i = 0; i < data.length; i++) {
		            $.ajax({
		                url: list_aJaxURL,
		                type: "POST",
		                data: "act=disable&id=" + data[i],
		                dataType: "json",
		                success: function (data) {
		                    if (data.error != "") {
		                        alert(data.error);
		                    }
		                }
		            });
		        }
		        //Reload Table
		        if ($.isFunction(window.LoadTable)) {
		            //execute it
		            LoadTable("prod_list");
		        }
		    });
		    $("#add_button1").on("click", function () {
		        $.ajax({
		            url: list_aJaxURL,
		            type: "POST",
		            data: "act=get_add_page",
		            dataType: "json",
		            success: function (data) {
		                if (typeof (data.error) != "undefined") {
		                    if (data.error != "") {
		                        alert(data.error);
		                    } else {
		                        $("#add-edit-prod-form").html(data.page);
		                        if ($.isFunction(window.LoadDialog)) {
		                            //execute it
		                            LoadDialog("add-edit-prod-form");
		                        }
		                    }
		                }
		            }
		        });
		    });
		}


	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {
	    	param = new Object();

            //Action
	    	param.act		= "save_trans";

		    param.ob		= $("#object").val();
		    param.id		= $("#describe_id").val();
		    param.pe		= $("#person").val();
		    param.c			= $("#comment").val();

		    $.ajax({
		        url: aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined")
					{
						if(data.error != "")
						{
							alert(data.error);
						}else{
							LoadTable(fName);
							$("#save-dialog").button("disable");
						}
					}
			    }
		    });
		});

	    // Add - Save Production
	    $(document).on("click", "#add-production", function () {
	    	param = new Object();

	    	param.act		= "save_prod";

		    param.id		= $("#production_id").val();
		    param.l_id		= $("#describe_id").val();
		    param.tn		= $("#production").val();
		    param.tq		= $("#quantity").val();
		    param.rq		= $("#real_quantity").val();
		    param.c			= $("#prod_comment").val();

	    	if(param.tn == ""){
				alert("შეავსეთ პროდუქტის სახელი!");
			}else if(param.tq == ""){
				alert("შეავსეთ პროდუქტის რაოდენობა!");
			}else {
			    $.ajax({
			        url: list_aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined")
						{
							if(data.error != "")
							{
								alert(data.error);
							}else
							{
								LoadTable("prod_list");
				        		CloseDialog("add-edit-prod-form");
							}
						}
				    }
			    });
			}
		});

        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});

        $(document).on("click", "#end_describe", function (event) {
            var describe_id	= $("#describe_id").val();
        	var check		= CheckProdList(describe_id);

        	if(check){
    			param 			= new Object();
    	    	param.act		= "end_describe";
    	    	param.id		= $("#describe_id").val();
    		    param.ob		= $("#object").val();
    		    param.pe		= $("#person").val();
    		    param.c			= $("#comment").val();


            	$.ajax({
    		        url: aJaxURL,
    			    data: param,
    		        success: function(data) {
    					if(typeof(data.error) != "undefined")
    					{
    						if(data.error != "")
    						{
    							alert(data.error);
    						}else
    						{
    							LoadTable(tName);
    			        		CloseDialog("add-edit-form");
    						}
    					}
    			    }
    		    });
        	}

		});

        $(document).on("keydown", "#production", function (event) {
        	if (event.keyCode == $.ui.keyCode.ENTER){
				param 			= new Object();
		    	param.act		= "get_production_quantity";
		    	param.pn		= $("#production").val();
			    param.ob		= $("#object").val();

	        	$.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined")
						{
							if(data.error != ""){
								alert(data.error);
							}else{
								$("#quantity").val(data.quantity);
							}
						}
				    }
			    });
        	}
		});



        $(document).on("click", "#production-widget", function (event) {
			param 			= new Object();
	    	param.act		= "get_production_quantity";
	    	param.pn		= $("#production").val();
		    param.ob		= $("#object").val();

        	$.ajax({
		        url: aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined")
					{
						if(data.error != "")
						{
							alert(data.error);
						}else{
							$("#quantity").val(data.quantity);
						}
					}
			    }
		    });
		});

        function CheckProdList(describe_id) {
            var check = false;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=check_prod_list&id=" + describe_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
            	}
        	});
            return check;
        }

		/**
		* Get next auto incremented id
		* @return {int}   Returns a next id
		*/
		function GetLocalID(){
			var local_id;
			$.ajax({
		        url: aJaxURL,
        		async: false, //r-value
			    data: "act=get_local_id",
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							local_id = data.local_id;
						}
					}
			    }
		    });
	      	return local_id;
		}

        function ClearDB() {
            $.ajax({
                url: aJaxURL,
                data: "act=clear_db",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
                }
            });
        }

        $(document).on("change", "#object",function(){
        	$("#object").attr("disabled", "disabled");
        	//$("#person").attr("disabled", "disabled");
     	});

        $(document).on("change", "#person",function(){
        	$("#person").attr("disabled", "disabled");
     	});

        $(document).on("click", "#print_describe", function () {
	    	param = new Object();

            //Action
		    param.id	= $("#object").val();

	    	var link = $(this).attr("str");
	    	link = "server-side/print/describe.action.php?id=" + param.id;

	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
	    	newWin.focus();
	    	newWin.onload = function() {
	    		newWin.print();
		    }
	    });
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">აღწერა</h2>
            	<div id="button_area">
        			<button id="add_button">ახალი აღწერა</button>
        		</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min" title="აღწერის თარიღი">აღწერის თარიღი</th>
                            <th style="width: 100%;">ობიექტი</th>
                            <th style="width: 100%;">აღმწერი</th>
                            <th class="min">სტატუსი</th>

                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_date" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_start_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_status" value="ფილტრი" class="search_init" />
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>

    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="აღწერა">
    	<!-- aJax -->
	</div>
</body>
</html>